
#include "../libpatches.h"
#include "shortout.h"
#include "../garbage.h"
#include <stdlib.h>
#include <stdio.h>
#include <AudioToolbox/AudioToolbox.h>

static void module_shortout_processor(module m);
void module_shortout_customizer (module m, char * name, char * value);

void module_shortout_initializer(module m) {
	m->customizer = module_shortout_customizer;
	m->processor = module_shortout_processor;
}

void module_shortout_customizer (module m, char * name, char * value) {
	extern int output_channels;
	extern AudioUnitSampleType * output_samples;
	customizer_count_inputs(m, name, value);
	if (m->input_count) {
		m->storage = (void *) new_garbage(m->input_count, sizeof(short));
		output_channels = m->input_count; 
		output_samples = (AudioUnitSampleType *) m->storage;
	}
}

static void module_shortout_processor(module m) {
	input * i = m->inputs;
	int count = m->input_count;
	AudioUnitSampleType * s = (AudioUnitSampleType *) m->storage;

	/*
	 * magic trick to convert to iphone audio samples:
	 * AudioUnitSampleType sample = wave * (1 << kAudioUnitSampleFractionBits);
	fprintf(stderr, "processing shortouter\n");
	*/
	for (;count--; i++, s++)
		*s = ***i * (1 << kAudioUnitSampleFractionBits);

}
